const NewsService = require('../../services/admin/NewsService');
const JWT = require('../../utils/JWT');

const NewsController = {
  // 添加
  add: async (req, res) => {
    const { title, content, category, cover, editorType = 0, isTop = 0, isPublish = 0 } = req.body;
    try {
      // 解析token
      const { authorization = '' } = req.headers;
      const token = authorization.split(' ')[1];
      const author = JWT.verifyToken(token)._id;

      const result = await NewsService.add({
        author,
        title,
        content,
        category,
        cover,
        editorType,
        isTop,
        isPublish,
      });
      if (!result) {
        res.send({
          success: false,
          code: 0,
          msg: '添加失败',
        });
      } else {
        res.send({
          data: result,
          success: true,
          code: 1,
          msg: '添加成功',
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '添加失败' + error,
      });
    }
  },
  // 修改
  edit: async (req, res) => {
    try {
      const { _id, title, content, category, cover, editorType = 0 } = req.body;
      const result = await NewsService.edit(_id, {
        title,
        content,
        category,
        cover,
        editorType,
      });
      if (!result) {
        res.send({
          success: false,
          code: 0,
          msg: '修改失败',
        });
      } else {
        res.send({
          data: result,
          success: true,
          code: 1,
          msg: '修改成功',
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '修改失败' + error,
      });
    }
  },
  // 删除
  delete: async (req, res) => {
    const { _id } = req.body;
    try {
      const result = await NewsService.delete(_id);
      if (!result) {
        res.send({
          success: false,
          code: 0,
          msg: '删除失败',
        });
      } else {
        res.send({
          success: true,
          code: 1,
          msg: '删除成功',
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '删除失败' + error,
      });
    }
  },
  // 发布&停止发布
  publish: async (req, res) => {
    try {
      const { _id, isPublish } = req.body;
      const result = await NewsService.publish(_id, isPublish);
      if (!result) {
        res.send({
          success: false,
          code: 0,
          msg: '发布失败',
        });
      } else {
        res.send({
          success: true,
          code: 1,
          msg: '发布成功',
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '发布失败' + error,
      });
    }
  },
  // 置顶&取消置顶
  isTop: async (req, res) => {
    try {
      const { _id, isTop } = req.body;
      const result = await NewsService.isTop(_id, isTop);
      if (!result) {
        res.send({
          success: false,
          code: 0,
          msg: '置顶失败',
        });
      } else {
        res.send({
          success: true,
          code: 1,
          msg: '置顶成功',
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '置顶失败' + error,
      });
    }
  },
  // 列表
  list: async (req, res) => {
    try {
      const { page = 1, pageSize = 10 } = req.body;
      // const { authorization = '' } = req.headers;
      // const token = authorization.split(' ')[1];
      // const author = JWT.verifyToken(token)._id;
      // 查询自己发布的文章
      if (!page || !pageSize || page < 0) {
        res.send({
          success: false,
          code: 0,
          msg: '参数错误',
        });
        return;
      }
      const result = await NewsService.list(req.body);
      if (!result) {
        res.send({
          success: false,
          code: 0,
          msg: '查询失败',
        });
      } else {
        res.send({
          data: result,
          success: true,
          code: 1,
          msg: '查询成功',
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '查询失败' + error,
      });
    }
  },
  //根据id查询
  findById: async (req, res) => {
    try {
      const { _id } = req.body;
      const result = await NewsService.findById(_id);
      if (!result) {
        res.send({
          success: false,
          code: 0,
          msg: '查询失败',
        });
      } else {
        res.send({
          data: result,
          success: true,
          code: 1,
          msg: '查询成功',
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '查询失败' + error,
      });
    }
  },
};

module.exports = NewsController;
